package mx.ags.miforo.dao;

import java.util.Iterator;
import java.util.List;
import mx.ags.miforo.bean.Topico;
import mx.ags.miforo.bean.Usuario;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class TopicoDaoImpl {


    public Integer Add(){
	  
	  Session session = DaoFactory.getFactory().openSession();
      Transaction tx = null;
      Integer topicoID = null;
      try{
         tx = session.beginTransaction();
         Topico topic = new Topico();
         topicoID= (Integer) session.save(topic); 
         tx.commit();
      }catch (HibernateException e) {
         if (tx!=null) tx.rollback();
         e.printStackTrace(); 
      }finally {
         session.close(); 
      }
      return topicoID;
	  
  }
	
	public void List(){
	     Session session = DaoFactory.getFactory().openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         List topicos = session.createQuery("FROM Topico").list(); 
	         for (Iterator iterator = 
	                           topicos.iterator(); iterator.hasNext();){
	            Topico topico = (Topico) iterator.next(); 
	            System.out.print("Tema: " + topico.getTema()); 
	            System.out.print(" Solucion: " + topico.getSolucion()); 
	            System.out.println(" Tecnologia: " + topico.getTecnologia()); 
	         }
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
		
	}
	
	public void Delete(Integer TopicoId){
	    Session session = DaoFactory.getFactory().openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         Topico topico = 
	                   (Topico)session.get(Usuario.class,TopicoId ); 
	         session.delete(topico); 
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
		
	}
	
	public void Update(Integer TopicoId){
		
		  Session session = DaoFactory.getFactory().openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         Topico topic = 
	                    (Topico)session.get(Topico.class, TopicoId); 
	          topic.setSolucion("asfdfdf");
	         session.update(topic); 
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
		
	}
	
}
